package com.pet.server.bo.common;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.pet.framework.page.Page;
import com.pet.framework.util.Config;
import com.pet.framework.util.FileOperate;
import com.pet.framework.util.NumberUtil;
import com.pet.framework.util.StringUtil;
import com.pet.server.bo.common.CommonService;
import com.pet.server.dao.common.ImageDao;
import com.pet.server.dao.common.ProductDao;
import com.pet.server.http.bean.ProductImg;
import com.pet.server.po.Image;
import com.pet.server.po.ProductData;
import com.pet.server.po.ProductInfo;
import com.pet.server.po.ProductView;
import com.pet.system.constants.ATTSystem;
import com.pet.system.util.FolderManager;

public class ProductService extends CommonService {

	public ProductDao productDao;
	
	public ImageDao imageDao;
	
	public ImageDao getImageDao() {
		return imageDao;
	}

	public void setImageDao(ImageDao imageDao) {
		this.imageDao = imageDao;
	}

	public ProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
	//保存产品
	public void saveProductInfo(ProductInfo product,File imgFile,String imgFileFileName,File image1,File image2,File image3,String image1FileName,String image2FileName,String image3FileName){
		String path1 = FolderManager.uploadFile(image1,image1FileName);
		String path2 = FolderManager.uploadFile(image2,image2FileName);
		String path3 = FolderManager.uploadFile(image3,image3FileName);
		String  paths[]={path1,path2,path3};
		if(null!=product){
			if(null!=imgFile){
				String path = FolderManager.getSysUserFolder();
				String filename=FolderManager.getCurrentName(1000, imgFileFileName);
				String resPath = FileOperate.uploadFile(imgFile, path, filename);
				product.setIcoPath(Config.getValue("ftp.url")+resPath);
			}
			product.setCreateDate(new Date());
			product.setState(ATTSystem.LOGIC_TRUE);
			saveObject(product);// creatObject(product);
			
			for(int i=0;i<paths.length;i++){
				if(!StringUtil.isEmpty(paths[i])){
					Image img=new Image();
					img.setOid(product.getId());
					img.setType(ATTSystem.PRODUCT_INFO);
					img.setSrc(paths[i]);
					img.setCreateTime(new Date());
					saveObject(img);
				}
			}
			
		}
	}
	//更新产品
	public void updateProductInfo(ProductInfo op,ProductInfo np,File imgFile,String imgFileFileName,File image1,File image2,File image3,String image1FileName,String image2FileName,String image3FileName){
		String path1 = FolderManager.uploadFile(image1,image1FileName);
		String path2 = FolderManager.uploadFile(image2,image2FileName);
		String path3 = FolderManager.uploadFile(image3,image3FileName);
		String  paths[]={path1,path2,path3};
		if(null!=imgFile){
			String path = FolderManager.getSysUserFolder();
			String filename=FolderManager.getCurrentName(1000, imgFileFileName);
			String resPath = FileOperate.uploadFile(imgFile, path, filename);
			op.setIcoPath(Config.getValue("ftp.url")+resPath);
		}
		op.setProductCate(np.getProductCate());
		op.setSellerId(np.getSellerId());
		op.setProductName(np.getProductName());
		op.setProductPrice(np.getProductPrice());
		op.setSalePrice(np.getSalePrice());
		op.setPetCate(np.getPetCate());
		op.setSource(np.getSource());
		op.setProductAge(np.getProductAge());
		op.setProductHref(np.getProductHref());
		op.setClientUrl(np.getClientUrl());
		op.setProductBrand(np.getProductBrand());
		op.setProductDescription(np.getProductDescription());
		op.setProductDetail(np.getProductDetail());
		updateObject(op);
		
		//deleteObject(np.getId());
		//System.out.println("op.getId()="+op.getId());
		imageDao.deleteOid(op.getId());
		for(int i=0;i<paths.length;i++){
			if(!StringUtil.isEmpty(paths[i])){
				Image img=new Image();
				img.setOid(op.getId());
				img.setType(ATTSystem.PRODUCT_INFO);
				img.setSrc(paths[i]);
				img.setCreateTime(new Date());
				saveObject(img);
			}
		}
	}
	
	
	//更新产品
	public void updateProductAlima(ProductData op,ProductData np,File imgFile,String imgFileFileName){
		if(null!=imgFile){
			String path = FolderManager.getSysUserFolder();
			String filename=FolderManager.getCurrentName(1000, imgFileFileName);
			String resPath = FileOperate.uploadFile(imgFile, path, filename);
			op.setIcoPath(Config.getValue("ftp.url")+resPath);
		}
		op.setProductCate(np.getProductCate());
		op.setSellerId(np.getSellerId());
		op.setProductName(np.getProductName());
		op.setProductPrice(np.getProductPrice());
		op.setSalePrice(np.getSalePrice());
		op.setPetCate(np.getPetCate());
		op.setSource(np.getSource());
		op.setProductAge(np.getProductAge());
		op.setProductHref(np.getProductHref());
		op.setClientUrl(np.getClientUrl());
		op.setProductBrand(np.getProductBrand());
		op.setProductDescription(np.getProductDescription());
		op.setState(np.getState());
		updateObject(op);
	}
	
	public void searchProductAlima(ProductData pd,Page page){
		productDao.searchProductAlima(pd, page);
	}
	
	public void searchProductInfo(ProductInfo product,Page page){
		productDao.searchProductInfo(product, page);
	}
	
	
	public void searchProductView(ProductInfo product,ProductView view,Page page){
	    productDao.searchProductView(product, view, page);
	}
	public void saveProductView(ProductView pv,Integer productId){
		    pv.setProductId(productId);
		    pv.setSeq(NumberUtil.getInteger(pv.getSeq(),0));
		    pv.setState(ATTSystem.LOGIC_TRUE);
			saveObject(pv);
	}
	public void updateProductView(ProductView opv,ProductView npv){
		opv.setImgPath(npv.getImgPath());
		opv.setTextTitle(npv.getTextTitle());
		opv.setTextContent(npv.getTextContent());
		opv.setViewChannel(npv.getViewChannel());
		opv.setViewType(npv.getViewType());
		opv.setSeq(npv.getSeq());
		opv.setIsDsp(npv.getIsDsp());
		opv.setDspName(npv.getDspName());
		opv.setNum(NumberUtil.getInteger(npv.getNum(), 1));
		updateObject(opv);
	}
	public void searchDateReport(String productName,Integer productId,Date beginDate,Date endDate,Page page){
		productDao.searchDateReport(productName, productId, beginDate, endDate, page);
	}
	public void searchAdvise(Page page){
		productDao.searchAdvise(page);
	}

	public ProductImg getImg(Integer id) {
		List<Image> list = imageDao.queryeOption(id);
		ProductImg pi=new ProductImg();
		if(list!=null){
			for(int i=0;i<list.size();i++){
				System.out.println("==="+list.get(i));
				Image img=list.get(i);
				
				if(i==0){
					pi.setImage1(img.getSrc());
				}
				if(i==1){
					pi.setImage2(img.getSrc());
				}
				if(i==2){
					pi.setImage3(img.getSrc());
					break;
				}
				
			}
			
		}
		return pi;
	}
}
